<template>
  <div>
    <el-table
      :data="initPermissionList"
      style="width: 95%; margin: 20px"
      row-key="id"
      border
      :expand-row-keys="['1']"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="名称" width="180"> </el-table-column>
      <el-table-column prop="code" label="权限值" width="180">
      </el-table-column>
      <el-table-column prop="toCode" label="跳转权限值"> </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-plus"
            @click="addPermission(row)"
          ></el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="updataPermission(row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deletePermission(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改菜单弹窗 -->
    <el-dialog
      :title="ruleForm.id ? '修改菜单' : '添加菜单'"
      width="50%"
      :visible.sync="dialogVisible"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="功能权限值" prop="code">
          <el-input v-model="ruleForm.code"></el-input>
        </el-form-item>
        <el-form-item label="跳转路由权限值" v-show="currnentLevel > 2">
          <el-input v-model="ruleForm.toCode"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <span>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >确认</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqGetPermissionList,
  reqAddPermission,
  reqUpdataPermission,
  reqDeletePermission,
} from "@/api/auth/permission";
export default {
  name: "Permission",
  data() {
    return {
      initPermissionList: [], //初始化菜单数据,
      dialogVisible: false, //控制弹窗显示和隐藏
      currnentLevel: null,
      ruleForm: {
        //需要添加的数据
        name: "",
        code: "",
        toCode: "",
        level: null,
        pid: null,
        type: null,
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [
          { required: true, message: "请输入功能权限值", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //初始化获取菜单数据
    async initData() {
      try {
        const result = await reqGetPermissionList();
        console.log(result.children);
        this.initPermissionList = result.children;
      } catch (error) {
        this.$message({
          message: error.message,
          type: "error",
        });
      }
    },
    //添加菜单
    addPermission(row) {
      this.ruleForm = {
        name: "",
        code: "",
        toCode: "",
        level: null,
        pid: null,
        type: null,
      };
      this.dialogVisible = true;
      this.currnentLevel = row.level;
      this.ruleForm.level = row.level + 1;
      this.ruleForm.pid = row.id;
      this.ruleForm.type = this.ruleForm.level === 4 ? 2 : 1;
    },
    //修改菜单事件按钮
    updataPermission(row) {
      this.ruleForm = row;
      this.dialogVisible = true;
      this.currnentLevel = row.level;
      this.ruleForm.level = row.level + 1;
      this.ruleForm.pid = row.id;
      this.ruleForm.type = this.ruleForm.level === 4 ? 2 : 1;
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            await reqUpdataPermission(this.ruleForm);
            this.$message({
              message: "恭喜你，修改菜单权限成功",
              type: "success",
            });
          } else {
            await reqAddPermission(this.ruleForm);
            this.$message({
              message: "恭喜你，添加菜单权限成功",
              type: "success",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
        this.dialogVisible = false;
        this.initData();
        console.log(this.ruleForm.level);
      });
    },
    //取消提交事件回调
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    //点击删除某一个菜单
    async deletePermission(id) {
      try {
        await reqDeletePermission(id);
        this.$message({
          message: "恭喜你，删除菜单成功！",
          type: "success",
        });
        this.initData();
      } catch (error) {
        this.$message({
          message: error.message,
          type: "error",
        });
      }
    },
  },
  mounted() {
    this.initData(); //初始化获取菜单数据
  },
};
</script>

<style lang="scss" scoped></style>
